package com.grape.grapeboot.tomcat;

import com.grape.grapeboot.servlet.IndexServet;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;

/**
 * 使用Java语言创建Tomcat服务器
 * Tomcat 底层执行程序 最终servlet
 * SpringMVC底层使用servlet 包装
 * @author: myx
 * @date: 2019/7/8 0008
 * Copyright © 2019-grape. All rights reserved.
 */
public class TomcatTest {
    // 端口号
    private static int PORT = 8080;
    // 项目名称
    private static String CONTEXTPATH = "/grapeboot";

    public static void main(String[] args) throws LifecycleException {
        // 创建Tomcat服务器
        Tomcat tomcatServer = new Tomcat();
        // 设置Tomcat端口号
        tomcatServer.setPort(PORT);
        tomcatServer.getHost().setAutoDeploy(false);
        // 创建Context上下文
        StandardContext standardContext = new StandardContext();
        standardContext.setPath(CONTEXTPATH);
        // 监听上下文
        standardContext.addLifecycleListener(new Tomcat.FixContextListener());
        // tomcat容器添加standardContext
        tomcatServer.getHost().addChild(standardContext);
        // 创建servlet
        tomcatServer.addServlet(CONTEXTPATH, "IndexServet", new IndexServet());
        // 添加servleturl映射
        standardContext.addServletMappingDecoded("/index", "IndexServet");
        tomcatServer.start();
        System.out.println("tomcat启动成功...");
        // 异步进行接收请求
        tomcatServer.getServer().await();
    }
}
